package com.example.booklist.controller;


import com.example.booklist.constant.UserConstant;
import com.example.booklist.model.UserInfo;
import com.example.booklist.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(UserInfo userInfo, HttpSession session) {
        if(!StringUtils.hasLength(userInfo.getUsername()) || !StringUtils.hasLength(userInfo.getPassword())) {
            return false;
        }
        UserInfo user = userService.getUser(userInfo);
        if(user == null) {
            return false;
        }
        user.setPassword("");
        session.setAttribute(UserConstant.SESSION_USER_KEY,userInfo);
        return true;
    }

}
